<?php
// 本类由系统自动生成，仅供测试用途
   
class PostAction extends CommonAction{
	var $url = "__APP__"; 
	public function index() { 
		  //echo "建设中";
	}	
	public function chk(){
		if(Cookie::get('acc_name')==""){
			$this->assign('jumpUrl',"__APP__/Public/login"); 
			$this->error("请登录!");
			return false;
		}
		else{
			$name = Cookie::get('acc_name');
			$name_array = d('guild_member')->where("acc_name ='".$name."'")->find();
			
			if($name_array['glevel'] != "会长"){
				$this->assign('jumpUrl',"__APP__"); 
				$this->error("您没有权限!");
				return false;
			}
		}		
	}
	

	
	public function gbook(){
		
		$guestbook_arr['user'] = Cookie::get('acc_name');
		
		$name_array = d('guild_member')->where("acc_name ='".$guestbook_arr['user']."'")->find();
		if($name_array['glevel'] == "会长"){
			$guestbook_arr['glevel'] = "会长";
		}
		import("ORG.Util.Page");
		$gbook = d('guestbook');
		
		$count = $gbook->count();
		$p  = new Page ($count, 5 );
		
		$p->setConfig('header','篇记录');
        $p->setConfig('prev',"<");
        $p->setConfig('next','>');
        $p->setConfig('first','<<');
        $p->setConfig('last','>>');
	    
       	$guestbook_arr['page'] = $p->getpage(); 
            
		$guestbook_arr['gbook'] = $gbook->order('gtime desc')->limit($p->firstRow.','.$p->listRows)->findAll();
		foreach($guestbook_arr['gbook'] as $k=>$v ){
			$guestbook_arr['gbook'][$k]['gtime'] = date("Y-m-d H:i:s",$v['gtime']);
		}
		//dump($guestbook_arr['gbook']);
		$this->ajaxReturn($guestbook_arr,"",1);
	}
	
	
	
	
	public function p_post(){
		if(Cookie::get('acc_name')==""){
			$this->ajaxReturn("","请登录后留言","0");			
		}
		else{		
			$guestbookObj = d("guestbook");
			$g_arr['contents'] = stripslashes($_POST['contents']);
			$g_arr['gtime'] = time();
			$g_arr['acc_name'] = Cookie::get('acc_name');
			//dump($g_arr);
			if($g_arr['contents'] && $guestbookObj->add($g_arr)){
				//$sql = $guestbookObj->getLastSql();
				//echo $sql;
				$g_arr['gtime'] = date("Y-m-d H:i:s",$g_arr['gtime']);
				$this->ajaxReturn($g_arr,"留言成功","1");
				//$this->success("留言成功！", spUrl("main","index"));
			}
			else{
				$this->ajaxReturn("","留言添加失败","0");
			}
		}
	}
	
	public function post_del(){
		$this->chk();
		if($_GET['gid']){
			$gid = $_GET['gid'];
			if(d("guestbook")->where("gid = '".$gid."'")->delete()){
				$this->assign('jumpUrl',"__APP__"); 
				$this->success("删除成功!");
			}
		}
		
		
	}

}
?>